package com.wanbangee.marksystem.service.imp;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.wanbangee.marksystem.dao.MExamDao;
import com.wanbangee.marksystem.dao.MExamMarkerDao;
import com.wanbangee.marksystem.dao.MSubjectDao;
import com.wanbangee.marksystem.entities.MExam;
import com.wanbangee.marksystem.entities.MExamMarker;
import com.wanbangee.marksystem.entities.MSubject;
import com.wanbangee.marksystem.service.ProgressCheckService;

@Service
public class ProgressCheckServiceImp implements ProgressCheckService {
	
	
	@Autowired
	private MExamMarkerDao mExamMarkerDao;
	
	@Autowired
	private MExamDao mExamDao;
	
	@Autowired
	private MSubjectDao mSubjectDao;
	
	/**
	 * 进度列表显示
	 */
	@Override
	public Map selectProgressList(Integer examId, Integer examSubId) {
		List<MExamMarker> mExamMarkers = mExamMarkerDao.selectMExamMarkerByExamIdAndExamSubId(examId, examSubId);
		if(mExamMarkers==null || mExamMarkers.size()==0) return null;
		HashMap<String, List<MExamMarker> >map = new HashMap();
		for (MExamMarker mExamMarker : mExamMarkers) {
			if(map.containsKey(mExamMarker.getTopicNo())) {
				map.get(mExamMarker.getTopicNo()).add(mExamMarker);
			}else {
				ArrayList<MExamMarker> array = new ArrayList<>();
				array.add(mExamMarker);
				map.put(mExamMarker.getTopicNo(), array);
			}
		}
		return map;
	}

	@Override
	public List<MExam> selectExamId(Integer schId, String date) {
		return mExamDao.selectMExamBySchIdAndDate(schId, date);
	}

	@Override
	public List<MSubject> selectMSubjectByExamId(Integer examId) {
		return mSubjectDao.selectSubjectByExamId(examId);
	}

	@Override
	public Integer insertMExamMarker(MExamMarker mExamMarker) {
		return mExamMarkerDao.insertMExamMarker(mExamMarker);
	}

	/**
	 * 分解阅卷任务的候选人列表
	 * @return
	 */
	@Override
	public List<MExamMarker> selectExamTeaCandidate(Integer examId, Integer examSubId, Integer topicNo) {
		return mExamMarkerDao.selectMExamMarkerInfoByExamIdAndExamSubIdAndTopicNoOnTea(examId, examSubId, topicNo);
	}

	@Override
	public Integer updateMExamMarkerCount( Integer oldMarkerId,
			Integer topicCount, Integer newMarkerId) {
		System.out.println("---------------------------------------------");
		//1. 更新阅卷组表
		// 把旧的的数量相减一下
		// 把新的的数量添加一下
		int result1 = mExamMarkerDao.updateMExamMarkerByMarkerId(oldMarkerId, topicCount, 1);
		int result2 = mExamMarkerDao.updateMExamMarkerByMarkerId(newMarkerId, topicCount, 2);
		
		return result1==result2?1:0;
	}

	@Override
	public MExamMarker selectMExamMarkerByMarkerIdOnResolver(Integer markerId) {
		return mExamMarkerDao.selectMExamMarkerByMarkerIdOnResolver(markerId);
	}

	@Override
	public List<MExamMarker> selectMExamMarkersRepeatList(Integer teaId, Integer schId) {
		System.out.println("=======================================1");
		// 查询是阅卷人一还是阅卷人二
		List<MExamMarker> result = mExamMarkerDao.selectRepeatListByTeaIdSchId(teaId, schId);
		System.out.println("=======================================1"); 
		for (MExamMarker mExamMarker : result) {
			if(mExamMarker.getTeaId1()==teaId) {
				System.out.println(mExamMarker.getExamId());
				System.out.println(mExamMarker.getExamSubId());
				System.out.println(Integer.valueOf(mExamMarker.getTopicNo()));
				System.out.println(Integer.valueOf(mExamMarker.getMarkerId()));
				int num = mExamMarkerDao.selectAlreadyRepeatExamNum(mExamMarker.getExamId(), mExamMarker.getExamSubId(), Integer.valueOf(mExamMarker.getTopicNo()), Integer.valueOf(mExamMarker.getMarkerId()), 1);
				mExamMarker.setAlreadyRepeatExamNum(num);
			}else {
				mExamMarker.setAlreadyRepeatExamNum(mExamMarkerDao.selectAlreadyRepeatExamNum(mExamMarker.getExamId(), mExamMarker.getExamSubId(), Integer.valueOf(mExamMarker.getTopicNo()), Integer.valueOf(mExamMarker.getMarkerId()), 2));
			}
		} 
		
		return result;
	}
}
